package cn.iocoder.yudao.module.vos.controller.admin.secretaccount;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vos.controller.admin.secretaccount.vo.*;
import cn.iocoder.yudao.module.vos.dal.dataobject.secretaccount.SecretAccountDO;
import cn.iocoder.yudao.module.vos.service.secretaccount.SecretAccountService;

@Tag(name = "管理后台 - 密钥管理")
@RestController
@RequestMapping("/vos/secret-account")
@Validated
@TenantIgnore
public class SecretAccountController {

    @Resource
    private SecretAccountService secretAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建密钥管理")
    @PreAuthorize("@ss.hasPermission('vos:secret-account:create')")
    public CommonResult<Long> createSecretAccount(@Valid @RequestBody SecretAccountSaveReqVO createReqVO) {
        return success(secretAccountService.createSecretAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新密钥管理")
    @PreAuthorize("@ss.hasPermission('vos:secret-account:update')")
    public CommonResult<Boolean> updateSecretAccount(@Valid @RequestBody SecretAccountSaveReqVO updateReqVO) {
        secretAccountService.updateSecretAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除密钥管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vos:secret-account:delete')")
    public CommonResult<Boolean> deleteSecretAccount(@RequestParam("id") Long id) {
        secretAccountService.deleteSecretAccount(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除密钥管理")
                @PreAuthorize("@ss.hasPermission('vos:secret-account:delete')")
    public CommonResult<Boolean> deleteSecretAccountList(@RequestParam("ids") List<Long> ids) {
        secretAccountService.deleteSecretAccountListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得密钥管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vos:secret-account:query')")
    public CommonResult<SecretAccountRespVO> getSecretAccount(@RequestParam("id") Long id) {
        SecretAccountDO secretAccount = secretAccountService.getSecretAccount(id);
        return success(BeanUtils.toBean(secretAccount, SecretAccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得密钥管理分页")
    @PreAuthorize("@ss.hasPermission('vos:secret-account:query')")
    public CommonResult<PageResult<SecretAccountRespVO>> getSecretAccountPage(@Valid SecretAccountPageReqVO pageReqVO) {
        PageResult<SecretAccountDO> pageResult = secretAccountService.getSecretAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SecretAccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出密钥管理 Excel")
    @PreAuthorize("@ss.hasPermission('vos:secret-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSecretAccountExcel(@Valid SecretAccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SecretAccountDO> list = secretAccountService.getSecretAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "密钥管理.xls", "数据", SecretAccountRespVO.class,
                        BeanUtils.toBean(list, SecretAccountRespVO.class));
    }

    @PostMapping("/recharge")
    @Operation(summary = "密钥余额充值")
    @PreAuthorize("@ss.hasPermission('vos:secret-account:recharge')")
    public CommonResult<Boolean> recharge(@Valid @RequestBody SecretAccountRechargeReqVO reqVO) {
        secretAccountService.recharge(reqVO);
        return success(true);
    }

}